package com.molyfun.parents.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.R;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:10/18/16.
 */
public class ThreeTabBar extends LinearLayout implements View.OnClickListener{

    private int mChooseTab = ThreeTabChooseTab.BAR_ONE;

    public static interface ThreeTabChooseTab{
        public static final int BAR_ONE = 1001;
        public static final int BAR_TWO = 1002;
        public static final int BAR_THREE = 1003;
    }

    protected LayoutInflater layoutInflater;

    private ThreeBarClickListener listener;
    private Context context;
    private boolean inited;

    @InjectView(R.id.tab_one)
    TextView mTabOne;
    @InjectView(R.id.tab_two)
    TextView mTabTwo;
    @InjectView(R.id.tab_three)
    TextView mTabThree;

    public ThreeTabBar(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public ThreeTabBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public ThreeTabBar(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.widget_three_tab_choose_bar, this);
        ButterKnife.inject(this);
        mTabOne.setOnClickListener(this);
        mTabTwo.setOnClickListener(this);
        mTabThree.setOnClickListener(this);
    }

    public int getCurrentChooseTab(){
        return mChooseTab;
    }

    /**
     * init view
     *
     */
    public void initData(String tab1, String tab2, String tab3) {
        if(inited){
            return;
        }
        mTabOne.setText(getTabTextShow(tab1));
        mTabTwo.setText(getTabTextShow(tab2));
        mTabThree.setText(getTabTextShow(tab3));
        inited = true;
    }

    private String getTabTextShow(String tabText){
        if(!TextUtils.isEmpty(tabText)){
            if(tabText.length() <= 3){
                return tabText;
            }else if(tabText.length() == 4){
                return tabText.substring(0, 2) + "\n" + tabText.substring(2);
            }else if(tabText.length() >= 5){
                return tabText.substring(0, 3) + "\n" + tabText.substring(3);
            }
        }
        return "";
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tab_one:
                if(mChooseTab != ThreeTabChooseTab.BAR_ONE){
                    setTabClick(ThreeTabChooseTab.BAR_ONE);
                    if(listener != null){
                        listener.onThreeBarClick(ThreeTabChooseTab.BAR_ONE);
                    }
                }
                break;
            case R.id.tab_two:
                if(mChooseTab != ThreeTabChooseTab.BAR_TWO){
                    setTabClick(ThreeTabChooseTab.BAR_TWO);
                    if(listener != null){
                        listener.onThreeBarClick(ThreeTabChooseTab.BAR_TWO);
                    }
                }
                break;
            case R.id.tab_three:
                if(mChooseTab != ThreeTabChooseTab.BAR_THREE){
                    setTabClick(ThreeTabChooseTab.BAR_THREE);
                    if(listener != null){
                        listener.onThreeBarClick(ThreeTabChooseTab.BAR_THREE);
                    }
                }
                break;
        }
    }

    /**
     * only view
     * @param tab
     */
    public void setTabClick(int tab){
        if(tab == ThreeTabChooseTab.BAR_ONE){
            mTabOne.setBackgroundResource(R.mipmap.ic_tab1_click);
//            mTabOne.setTextColor(ContextCompat.getColor(context, R.color.color_white));
            mTabTwo.setBackgroundResource(R.mipmap.ic_tab2_normal);
//            mTabTwo.setTextColor(ContextCompat.getColor(context, R.color.color_title_text));
            mTabThree.setBackgroundResource(R.mipmap.ic_tab3_normal);
//            mTabThree.setTextColor(ContextCompat.getColor(context, R.color.color_title_text));
            mChooseTab = ThreeTabChooseTab.BAR_ONE;
        }else if(tab == ThreeTabChooseTab.BAR_TWO){
            mTabOne.setBackgroundResource(R.mipmap.ic_tab1_normal);
//            mTabOne.setTextColor(ContextCompat.getColor(context, R.color.color_white));
            mTabTwo.setBackgroundResource(R.mipmap.ic_tab2_click);
//            mTabTwo.setTextColor(ContextCompat.getColor(context, R.color.color_title_text));
            mTabThree.setBackgroundResource(R.mipmap.ic_tab3_normal);
            mChooseTab = ThreeTabChooseTab.BAR_TWO;
        }else{
            mTabOne.setBackgroundResource(R.mipmap.ic_tab1_normal);
//            mTabOne.setTextColor(ContextCompat.getColor(context, R.color.color_white));
            mTabTwo.setBackgroundResource(R.mipmap.ic_tab2_normal);
//            mTabTwo.setTextColor(ContextCompat.getColor(context, R.color.color_title_text));
            mTabThree.setBackgroundResource(R.mipmap.ic_tab3_click);
            mChooseTab = ThreeTabChooseTab.BAR_THREE;
        }
    }

    public void setThreeBarClickListener(ThreeBarClickListener listener) {
        this.listener = listener;
    }

    public static interface ThreeBarClickListener {
        void onThreeBarClick(int chooseTab);
    }

}